<?php

class AdvController extends Controller
{
	private $_model;
	private $_adModel;
	
	public function init(){
		parent::init();
		$this->defaultAction = 'howto';
	}
	
	public function actions()
    {
        return array(
            'deposit'=>'application.controllers.adv.DepositAction',
        	'ad_status_edit'=>'application.controllers.adv.Ad_status_editAction',
        	'beforeStatusChange'=>'application.controllers.adv.BeforeStatusChangeAction',
        	'ad'=>'application.controllers.adv.AdAction',
        	'edit'=>'application.controllers.adv.EditAction',
        );
    }
	
	public function actionRegister(){
		$model = $this->loadModel();
		if(isset($_POST['ajax']) && $_POST['ajax']==='ads-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		if(isset($_POST['AdsForm']))
		{
			$model->attributes = $_POST['AdsForm'];
			if($model->redefineParams() && $model->cropImage() && $model->validate()){
				$ad_model = new Ad;
				$ad_model->details = $model;
				if($ad_model->save()){
					$ad_model->details = $model;
					$this->redirect($this->createUrl('adv/deposit',array('ad_id'=>$ad_model->id)));
				}
			}
		}
		$this->render('register',array('model'=>$model));
	}
	
	public function actionBilling(){
		$this->render('billing');
	}
	
	public function actionAds(){
		$criteria=new CDbCriteria(array(
			'condition'=>'status!='.Ad::STATUS_REMOVED,
			'order'=>'date DESC',
		));
		$criteria->addSearchCondition('uid',Yii::app()->user->id,'=');
		$dataProvider=new CActiveDataProvider('Ad', array(
//			'pagination'=>array(
//				'pageSize'=>Yii::app()->params['adsPerPage'],
//			),
			'criteria'=>$criteria,
		));
		$this->render('ads',array('dataProvider'=>$dataProvider));
	}
	
	public function actionHowto(){
		$this->render('howto');
	}
	
	public function actionTempImage(){
		$model = $this->loadModel();
		$image = CUploadedFile::getInstance($model,'uploadImage');
		if(!$image)  Yii::app()->end();
		if(!$model->validate('uploadImage') && $model->hasErrors('uploadImage')) {
			echo json_encode(array('error'=>$model->getError('uploadImage')));
			Yii::app()->end();
		}
		$file_md5 = md5_file($image->getTempName());
		$file_name = $file_md5.'.'.$image->getName();
		if (is_object($image) && get_class($image)==='CUploadedFile')
		{
			if($image->saveAs(Yii::app()->params['tmpImg_path'].$file_name)){
				echo json_encode(array('res'=>'/tmp/'.$file_name));
			}else
				echo json_encode(array('error'=>$image->getError()));
		}else{
			echo json_encode(array('error'=>$image->getError()));
		}
	}
	
	public function loadModel(){
		if($this->_model===null)
		{
			$this->_model = new AdsForm;
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
	
	public function loadAdModel(){
		if($this->_adModel===null)
		{
			if(isset($_GET['ad_id']))
			{
				$this->_adModel=Ad::model()->findByPk($_GET['ad_id']);
			}
			if($this->_adModel===null || $this->_adModel->status == Ad::STATUS_REMOVED)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_adModel;
	}
}